﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

namespace ConsoleControl
{
    public class ProccessControler
    {
        private int _type=0;
        private TextBox _textbox;
        private RichTextBox _richbox;
        private string _app;
        private string _args;
        private delegate void AppendTextDelegate(string text);

        public ProccessControler(string app, string args,TextBox textbox)
        {
             if (textbox == null)
                return;

            _app = app;
            _args = args;
            _textbox = textbox;
            _type = 1;

        }

        public ProccessControler(string app, string args, RichTextBox richbox)
        {
            if (richbox == null)
                return;

            _app = app;
            _args = args;
            _richbox = richbox;
            _type = 2;

        }

        public void RunApp()
        {
            Process proc = new Process();
            proc.StartInfo.FileName = _app;
            proc.StartInfo.Arguments = _args;
            proc.StartInfo.UseShellExecute = false;

            // set up output redirection
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.EnableRaisingEvents = true;
            proc.StartInfo.CreateNoWindow = true;

            // Set the data received handlers
            proc.ErrorDataReceived += proc_DataReceived;
            proc.OutputDataReceived += proc_DataReceived;

            // Configure the process exited event
            proc.Exited += new EventHandler(ProcExited);

            proc.Start();
            proc.BeginErrorReadLine();
            proc.BeginOutputReadLine();

        }

        private void proc_DataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                AppendText(e.Data + Environment.NewLine);
            }
        }

        /// <summary>
        /// Actions to take when console process completes
        /// </summary>
        private void ProcExited(object sender, System.EventArgs e)
        {
            Process proc = (Process)sender;
            Thread.Sleep(40);

            if (proc.ExitCode == 0)
            {
                this.AppendText(_app + " Success." + Environment.NewLine);
            }
            else
            {
                this.AppendText(_app + " Failed." + Environment.NewLine);
            }

            proc.Close();
        }
        private void AppendText(string text)
        {
            if (_type == 1)
            {
                if (this._textbox.InvokeRequired)
                {
                    _textbox.Invoke(new AppendTextDelegate(this.AppendText), new object[] { text });
                }
                else
                {
                    _textbox.AppendText(text);
                }
            }
            else if(_type == 2)
            {
                if (this._richbox.InvokeRequired)
                {
                    _richbox.Invoke(new AppendTextDelegate(this.AppendText), new object[] { text });
                }
                else
                {
                    _richbox.AppendText(text);
                }
            }
        }
    }
}
